﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using Yahoo.Yui.Compressor;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Shut;


/// <summary>
/// Summary description for dev.generatecss
/// </summary>
/// 
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class dev_minify : System.Web.Services.WebService
{

	public dev_minify(){

	}

    [WebMethod]
	[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string MinifyCss(string cssText, string path) {
		// read what? read request
		
		
		string compressedCss = CssCompressor.Compress(cssText, 72, CssCompressionType.MichaelAshRegexEnhancements, true);
		// write on file
		string csspath = Server.MapPath(SiteSettings.Instance.CssLocation + path);
		File.WriteAllText(csspath,compressedCss);

		return VirtualPathUtility.ToAbsolute(SiteSettings.Instance.CssLocation + path);
        
    }
	//[WebMethod]
	//[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
	//public string MinifyRtlCss(string cssText)
	//{
		
	//    string compressedCss = CssCompressor.Compress(cssText, 72, CssCompressionType.MichaelAshRegexEnhancements, true);
	//    // write on file
	//    string csspath = Server.MapPath(SiteSettings.Instance.CssLocation + "sh.rtl.min.css");
	//    File.WriteAllText(csspath, compressedCss);
		
	//    return VirtualPathUtility.ToAbsolute(SiteSettings.Instance.CssLocation + "sh.rtl.min.css");

	//}

	[WebMethod]
	[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
	public string MinifyJs()
	{
		// grab all sh in one file, then grab all ui in one file
		string devFolder  = Server.MapPath(SiteSettings.Instance.DevLocation);
		string strOut = "";
		JArray shfiles = new JArray();
		
		foreach (string file in Directory.GetFiles(devFolder, "sh.*.js", SearchOption.TopDirectoryOnly))
		{
			strOut += File.ReadAllText(file);
			shfiles.Add(file);
		}
		
		// write on file
		string jspath = Server.MapPath(SiteSettings.Instance.JsLocation + "sh.min.js"); 
		string compressedjs = JavaScriptCompressor.Compress(strOut,false,true,true,false,72);
		File.WriteAllText(jspath, compressedjs);

		
		JArray uifiles = new JArray();
		strOut = "";
		foreach (string file in Directory.GetFiles(devFolder, "ui*.js", SearchOption.TopDirectoryOnly))
		{
			strOut += File.ReadAllText(file);
			uifiles.Add(file);
		}

		// write on file
		jspath = Server.MapPath(SiteSettings.Instance.JsLocation + "ui.min.js"); // TODO: version number
		compressedjs = JavaScriptCompressor.Compress(strOut, false, true, true, false, 72);
		File.WriteAllText(jspath, compressedjs);


		JObject shobjects = new JObject(
			 new JProperty("newPath", VirtualPathUtility.ToAbsolute(SiteSettings.Instance.JsLocation + "sh.min.js")),
			 new JProperty("Files", shfiles)
		);


		JObject uiobjects = new JObject(
			 new JProperty("newPath", VirtualPathUtility.ToAbsolute(SiteSettings.Instance.JsLocation + "ui.min.js")),
			 new JProperty("Files", uifiles)
		);

		
		JObject o = new JObject(
			new JProperty("ShMinified", shobjects),
			 new JProperty("UiMinified", uiobjects)
		  );
	
	
		
		return o.ToString();

	}
}
